package com.sss.springcloud.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sss.springcloud.common.Result;
import com.sss.springcloud.entities.Storage;
import com.sss.springcloud.service.IStorageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  库存扣减控制器
 * </p>
 *
 * @author sss
 * @since 2020-03-13
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Resource
    private IStorageService storageService;

    @PostMapping("/decrease")
    public Result decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count){
        int age=10/0;
        Storage storage = storageService.list().get(0);
      //  Integer newTotal=storage.getTotal() - count;
        Integer newUsed=storage.getUsed()+count;
        Integer newResidue=storage.getResidue() - count;
        Storage newStorage = new Storage();
       // newStorage.setTotal(newTotal);
        newStorage.setUsed(newUsed);
        newStorage.setResidue(newResidue);
        storageService.update(newStorage,new UpdateWrapper<Storage>().eq("product_id",productId));
        return Result.succes(200,"库存扣减成功");
    }

}

